<?php
namespace app\api\controller\revenue_ap;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\rebate\RebateList;
use app\common\model\rebate\Rebate as RebateModel;
use app\api\controller\Base;
use app\api\controller\Baseuser;
use app\common\model\revenue_ap\Raplist as RapListModel;
use app\common\model\revenue_ap\Rap as RapModel;

//挂账列表
class Raplist extends Baseuser
{
	//初始化
    public function initialize(){
		// Rolepower::powerguard(101004);
	}

	/**
	 * 增加/修改服务费信息
	 * @param  [type] $type  [description]
	 * @param  [type] $id    [description]
	 * @param  [type] $price [description]
	 * @param  string $img   [description]
	 * @param  string $note  [description]
	 * @param  string $time  [description]
	 * @return [type]        [description]
	 */
	public static function create($type,$id,$price,$img=null,$note=null,$time=null,$SettlementTime= null){
		$data['Type'] = $type;
		$data['CorrelationId'] = $id;
		$data['Price'] = $price;
		$data['Note'] = $note;
		$data['Img'] =  $img;
		$data['ExpectedTime'] =  $time;
		//判断是否存在挂账信息
		$sb = RapListModel::where('IsDelete','<>','1')->where('Type',$type)->where('CorrelationId',$id)->find();
        if(empty($sb)){
        	// 创建新记录
			$RAPListId = RapListModel::user_create($data);
			if(!empty($price)){
				RapModel::user_create(['id'=>$RAPListId,'Price'=>$price,'ExpectedTime'=>$time,'Note'=>$note,'Img'=>$img,'SettlementTime'=>$SettlementTime]);
			}
			return $RAPListId;
        }else{
        	//修改记录
        	$data['id'] = $sb['RAPListId'];
        	RapListModel::user_reinfo($data);
        	return $sb['RAPListId'];
        }
	}

	/**
	 * 修改服务费档案 relist
	 * @return [type] [description]
	 */
	public function relist(){
		$data = input('post.');
		if(empty($data['id'])){
            return moerr('缺少参数，请重新尝试');
        }
        $list = ['Note','Img','State','IsDelete','ExpectedTime'];

        $ndata = [];//需要调整的数据
        foreach ($list as $key => $value) {
        	if(isset($data[$value])){
        		$ndata[$value] = $data[$value];
        	}
        }
        if(empty($ndata)){
        	return myerr('没有找到需要调整的数据');
        }
        RapListModel::user_reinfo($ndata);
		return myok('修改成功');        
	}

	/**
	 * 查询服务费档案 getlist
	 * @return [type] [description]
	 */
	public function getlistinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = RebateList::getinfo($id);
		if(!$data['code']){
			return myerr($data['msg']);
		}
		$data = $data['msg'];
		return myok($data);
	}


	/**
	 * 记录服务费信息 createinfo 
	 * @return [type] [description]
	 */
	public function add_price(){
		//创建档案
		$data = input('post.');
		$id = input('id');//档案id
		//增加记录信息
		if(!empty($data)){
			RebateModel::user_create($id,$data);
		}
		//修改档案信息 合计服务费 每次服务费后将此值向上增加
		RebateList::add_price($id,$data['Price']);
		return myok();
	}

	/**
	 * 查询服务费记录具体信息 getreinfo
	 * @return [type] [description]
	 */
	public function getreinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$data = RebateModel::getinfo($id);
		return myok($data);
	}

	/**
	 * 修改服务费信息 reinfo
	 * @return [type] [description]
	 */
	public function reinfo(){
		$idata  = input('post.');
		if(empty($idata['id'])){
			return myerr('缺少参数，请重新尝试');
		}
        $list = ['Note','Img','State','IsDelete','Limit','Price'];
        $ndata = [];//需要调整的数据
        foreach ($list as $key => $value) {
        	if(isset($idata[$value])){
        		$ndata[$value] = $idata[$value];
        	}
        }
        if(empty($ndata)){
        	return myerr('没有找到需要调整的数据');
        }
        $ndata['id'] = $idata['id'];
        RebateModel::user_reinfo($ndata);
		return myok('修改成功');
	}

}
